package com.andan.playerpos;

import com.andan.playerpos.listener.Playerlistene;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public final class Playerpos extends JavaPlugin {

    static Playerpos instance;

    public static Playerpos getInstance(){
        return instance;
    }

    @Override
    public void onEnable() {
        // Plugin startup logic
        instance = this;
        loadcfg();
        getServer().getPluginManager().registerEvents(new Playerlistene(),this);

    }
    private void loadcfg () {
        File f = new File(getDataFolder() + "/config.yml");
        if (f.exists()) {
            getLogger().info("检测到配置文件，开始加载");
        } else {
            getLogger().info("未检测到配置，生成默认配置");
            saveDefaultConfig();
        }
        reloadConfig();
        getLogger().info("配置文件加载成功！");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        ///ppos give %player_name%
        ///ppos setitem

        if (args.length==2&&args[0].equals("give")){
            Player p = getServer().getPlayerExact(args[1]);
            if (p!=null&&p.isOnline()){
                p.getInventory().addItem(getConfig().getItemStack("setting.itemStack"));
                p.sendMessage(getConfig().getString("message.getitem"));
            }
        }
        if (args.length==1&&args[0].equals("set")&&sender.isOp()){
            Player p = (Player) sender;
            ItemStack itemInMainHand = p.getInventory().getItemInMainHand();
            if (itemInMainHand!=null){
                getConfig().set("setting.itemStack",itemInMainHand);
                saveConfig();
            }
        }

        return false;
    }
}
